/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.sttg.csp.common.struts;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.NoParameters;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
import com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor;

/**
 * @author $Author: syf $
 * @version $Revision: 1.1 $
 */
public class ParametersInterceptor implements Interceptor {
	/**
	 * Logger for this class
	 */
	private final Log logger = LogFactory.getLog(getClass());
	
	@SuppressWarnings("deprecation")
	public String intercept(ActionInvocation invocation) throws Exception {
		if (!(invocation.getAction() instanceof NoParameters)) {
			final Map parameters = ActionContext.getContext().getParameters();

			if (logger.isDebugEnabled()) {
				StringBuffer str = new StringBuffer();
				str.append("{");
				boolean isFirst = true;
				for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {
					if (isFirst == false) {
						str.append(",");
					} else {
						isFirst = false;
					}
					Map.Entry entry = (Map.Entry) i.next();
					String key = (String) entry.getKey();
					str.append(key).append("=");
					Object value = entry.getValue();
					if (value instanceof String[]) {
						String[] values = (String[]) entry.getValue();
						if (values.length == 1) {
							str.append(values[0].trim());
						} else {
							str.append("[");
							for (int n = 0; n < values.length; n++) {
								if (n != 0) {
									str.append(",");
								}
								str.append(values[n]);
							}
						}
					} else if (value instanceof File[]) {
						File[] values = (File[]) value;
						for (int n = 0; n < values.length; n++) {
							File f = values[n];
							str.append("[filename=" + f.getName() + ",path="
									+ f.getPath() + "]");
						}
					} else {
						str.append("[" + value.getClass().getName() + "]");
					}
				}
				str.append("}");
				logger.debug("Setting params " + str.toString());
			}

			try {
				invocation.getInvocationContext().put(
						InstantiatingNullHandler.CREATE_NULL_OBJECTS,
						Boolean.TRUE);
				invocation.getInvocationContext()
						.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION,
								Boolean.TRUE);
				invocation.getInvocationContext().put(
						XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

				if (parameters != null) {
					final OgnlValueStack stack = (OgnlValueStack) ActionContext
							.getContext().getValueStack();

					for (Iterator iterator = parameters.entrySet().iterator(); iterator
							.hasNext();) {
						Map.Entry entry = (Map.Entry) iterator.next();
						if (entry.getValue() instanceof String[]) {
							String[] values = (String[]) entry.getValue();
							if (values.length == 1) {
								stack.setValue(entry.getKey().toString(),
										values[0].trim());
							} else {
								stack.setValue(entry.getKey().toString(),
										entry.getValue());
							}
						} else {
							stack.setValue(entry.getKey().toString(),
									entry.getValue());
						}
					}
				}
			} finally {
				invocation.getInvocationContext().put(
						InstantiatingNullHandler.CREATE_NULL_OBJECTS,
						Boolean.FALSE);
				invocation.getInvocationContext().put(
						XWorkMethodAccessor.DENY_METHOD_EXECUTION,
						Boolean.FALSE);
				invocation.getInvocationContext().put(
						XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.FALSE);
			}
		}
		return invocation.invoke();
	}

	public void destroy() {

	}

	public void init() {

	}
}
